<?php

namespace Cps\Controller;

use Think\Controller;
use Home\Controller\CaptchaController;
use Think\Crypt;

/**
 * Description of LoginController
 *
 * @author xtceetg
 */
class LoginController extends Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->display();
    }

    public function login() {
        //var_dump($_POST);
        $username = I('post.username');
        $password = I('post.password');

        $captcha = I('post.captcha');
        if ($username == '' || $password == '' || $captcha == '') {
            $this->redirect('login/index');
        }

        $verify = new CaptchaController();
        if (!$verify->check_verify($captcha)) {
            $this->redirect('login/index');
        }
        $cpsModel = new \Cps\Model\CpsmemberModel();
        $cps = $cpsModel->getByCpsUser($username);
        if ($cps) {
            if (!Crypt::Crypt_Hash_Compare($cps['password'], $password)) {
                $this->error('用户名或密码错误！', U("login/index"));
            } else {
                //登录成功                    
                session("[start]");
                session('cpsinfo', $cps);
                $this->redirect('Index/index');
            }
        } else {
            $this->error('用户名不存在!', U("login/index"));
        }
    }

    public function logout() {
        session("[start]");
        $_SESSION['cpsinfo'] = null;
        unset($_SESSION['cpsinfo']);
        $this->redirect('index/index');
    }

    public function refer_login() {
//        if ($_SERVER['HTTP_REFERER'] != "http://www.248game.com/admin.php?s=/cps/index.html") {
//            $this->redirect("http://www.248game.com/");
//        }
        $username = I('get.username');
        $password = I('get.password');
        $cpsModel = new \Cps\Model\CpsmemberModel();
        $cps = $cpsModel->getByCpsUser($username);        
                
        if ($cps) {
            if ($cps['password']!=$password) {           
                $this->redirect("http://www.248game.com/");
            } else {
                //登录成功                                    
                session("[start]");
                session('cpsinfo', $cps);
                $this->redirect('Index/index');
            }
        } else {
            $this->redirect("http://www.248game.com/");
        }
    }

}
